$(function(){ 
	$("#sendMessage").on('click',function(){
		send();
	});
	chat();
    setInterval("chat()",15000);
    $(document).keydown(function(event){
		if(event.keyCode==13){ 
			send();
		} 
	});
});
function chat(){
	$(".talk_show").empty();
	var sequence =$("#sequence").val();
	var usersequence =$("#usersequence").val();
	$.ajax({
	   	 type : "post",
	        url : "/TeaCulture/chat/do",
	        data : {"usersequence" : usersequence , "sequence" : sequence},
	        dataType : "json",
	        traditional : true,
	        success : function(result){

	        	if(result.result.code === "1"){
	        		 var dataObj = result.chats; 
	        		 var tbody = "";
	                 $.each(dataObj, function(index, item){
	                	 var j = parseInt(item.create_dt);
                		 var newDate = new Date();
                		 newDate.setTime(j);
	                	 if(usersequence == item.usersequence){
	                		
	                		 tbody += "<div class='ctalk'>"+newDate.toLocaleString()+"</div><div class='atalk'><span class='asay'>"+item.nickname+":<br>"+item.message+"</span></div>";
	                	 }else{
	                		 tbody += "<div class='ctalk'>"+newDate.toLocaleString()+"</div><div class='btalk'><span class='bsay'>"+item.nickname+":<br>"+item.message+"</span></div>";
	                	 }
	                 });
	                 $(".talk_show").prepend(tbody);
	                 tbody == "";
	                $("#words img").each(function(i){
	                	if($(this).width()>400){
	                		$(this).addClass("divImg");
	            		}
	                });
	                $("#words img").click(function(){
	                	if($(this).width()>400){
	                		if($(this).attr('class') == "divImg"){
		                		$(this).removeClass("divImg");	                			
	                		}else{
		                		$(this).addClass("divImg");
	                		}
	            		}
	                });
	                $("#words").scrollTop($("#words")[0].scrollHeight);

                }else{
		        	layer.msg(result.result.des, {icon: 5});
	            }
	        },    
	        error : function(result){
	        	layer.msg(result.result.des, {icon: 5});
	        }
	   });
}

function send(){
 	var message = $(".w-e-text").html();
 	if(message === null || message === ""){
    	layer.msg("发送消息不能为空！", {icon: 5});
 		return;
 	}
	var sequence =$("#sequence").val();
	var usersequence =$("#usersequence").val();

	$.ajax({
	   	 type : "post",
	        url : "/TeaCulture/chat/send",
	        data : {"message" : message,  "usersequence" : usersequence , "sequence" : sequence},
	        dataType : "json",
	        traditional : true,
	        success : function(result){
	        	if(result.result.code === "1"){
	        		chat();
	        		$(".w-e-text").html("");
	        	}else{
		        	layer.msg(result.result.des, {icon: 5});
	            }
	        },    
	        error : function(result){
	        	layer.msg(result.result.des, {icon: 5});
	        }
	   });
}